package kr.hkit.powerjava.chapter22.work2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

import kr.hkit.powerjava.chapter22.work2.dao.DepartmentManagerImpl;
import kr.hkit.powerjava.chapter22.work2.dao.StudentManagerImpl;
import kr.hkit.powerjava.chapter22.work2.vo.Department;
import kr.hkit.powerjava.chapter22.work2.vo.Student;

public class TestMain {

	public static void main(String[] args) {
		ArrayList<Student> stdList = new ArrayList<>();
		StudentManagerImpl sm = new StudentManagerImpl(stdList);

		// 추가
		Student std1 = new Student(1, "김태희", 90, 90, 90);
		sm.addItem(std1);
		Student std2 = new Student(2, "제시카", 89, 79, 77);
		sm.addItem(std2);

		// 삭제
		sm.delItem(std1);
		sm.delItem(std2);
		sm.delItem(std2);

		// 검색을 위해 다시 추가
		sm.addItem(std1);
		sm.addItem(std2);
		sm.addItem(new Student(3, "김주하", 80, 80, 80));
		sm.addItem(new Student(4, "이민정", 79, 79, 79));

		// 검색
		Student searchItem = new Student(1, "김태희");
		String isSearch = sm.searchItem(searchItem) ? "존재" : "없음";
		System.out.println(searchItem.getStdNum() + ":"
				+ searchItem.getStdName() + " = " + isSearch);
		
		searchItem = new Student(2, "김태희");
		isSearch = sm.searchItem(searchItem) ? "존재" : "없음";
		System.out.println(searchItem.getStdNum() + ":"
				+ searchItem.getStdName() + " = " + isSearch);
		// null인 이유 = equals overide하지 않아서

		// 수정
		Student updateItem = new Student(1, "김태희", 0, 93, 94);
		sm.updateItem(updateItem);

		// 출력
		sm.prnItems();
		System.out.println("총점순 정렬: 내림차순");
		Comparator<Student> totalCom = new Comparator<Student>() {
			
			@Override
			public int compare(Student o1, Student o2) {
				return (int) (o2.getTotal() - o1.getTotal());
			}
		};
		sm.prnItems(totalCom);
		
		System.out.println("이름순 정렬: 오름차순");
		Comparator<Student> com = new Comparator<Student>() {
			
			@Override
			public int compare(Student o1, Student o2) {
				return o1.getStdName().hashCode() - o2.getStdName().hashCode();
			}
		};
		sm.prnItems(com);
		
		System.out.println("국어점수 정렬: 내림차순");
		sm.prnItems(new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				return o2.getKor() - o1.getKor();
			}
		});
		
		
		ArrayList<Department> deptList = new ArrayList<>();
		DepartmentManagerImpl dm = new DepartmentManagerImpl(deptList);
		
		//추가
		Department dept = new Department(1, "컴공", 2);
		dm.addItem(dept);
		dept = new Department(2, "화공", 5);
		dm.addItem(dept);
		dept = new Department(3, "기계", 3);
		dm.addItem(dept);
		
		//삭제
		dm.delItem(dept);
		
		dm.addItem(dept);
		
		//검색
		Department searchDept = new Department(1, "컴공");
		isSearch = dm.searchItem(searchDept) ? "존재" : "없음";
		System.out.println(searchDept.getDeptNo() + ":"
				+ searchDept.getDeptName() + " = " + isSearch);
		
		//수정
		Department updateDept = new Department(1, "컴공", 1);
		dm.updateItem(updateDept);
		
		//출력
		dm.prnItems();
		
		System.out.println("층단위 정렬");
		dm.prnItems(new Comparator<Department>() {
			
			@Override
			public int compare(Department o1, Department o2) {
				return o1.getFloor() - o2.getFloor();
			}
		});
		
		System.out.println("학과명 정렬");
		dm.prnItems(new Comparator<Department>() {
			
			@Override
			public int compare(Department o1, Department o2) {
				return o1.getDeptName().hashCode() - o2.getDeptName().hashCode();
			}
		});
		
		System.out.println("학과번호 정렬");
		dm.prnItems(new Comparator<Department>() {
			
			@Override
			public int compare(Department o1, Department o2) {
				return o1.getDeptNo() - o2.getDeptNo();
			}
		});

		for(Student std: stdList){
			System.out.println(std);
		}
		
		Collections.shuffle(stdList);
		System.out.println("------------------반복자---");
		Student std = null;
		Iterator<Student> iterator = stdList.iterator();
		while(iterator.hasNext()){
			std = iterator.next();
			System.out.println(std);
		}
	}
}
